import 'package:flutter/material.dart';
import './trade/index.dart';
import './wallet/wallet_page.dart';
import './home/home_page.dart';
import './invest/index.dart';
import './my/index.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/generated/l10n.dart';
bool shortenOn = false;

class TabMainPage extends StatefulWidget {
  @override
  _PagesState createState() => _PagesState();
}

class _PagesState extends State<TabMainPage> {
  int _currentIndex = 0;
  bool darkEnabled = false;
  String themeMode;
  bool darkOLED;

  toggleTheme() {
    switch (themeMode) {
      case "Automatic":
        themeMode = "Dark";
        break;
      case "Dark":
        themeMode = "Light";
        break;
      case "Light":
        themeMode = "Automatic";
        break;
    }
    handleUpdate();
    savePreferences();
  }

  setDarkEnabled() {
    switch (themeMode) {
      case "Automatic":
        int nowHour = new DateTime.now().hour;
        if (nowHour > 6 && nowHour < 20) {
          darkEnabled = false;
        } else {
          darkEnabled = true;
        }
        break;
      case "Dark":
        darkEnabled = true;
        break;
      case "Light":
        darkEnabled = false;
        break;
    }
    setNavBarColor();
  }

  handleUpdate() {
    setState(() {
      setDarkEnabled();
    });
  }

  switchOLED({state}) {
    setState(() {
      darkOLED = state ?? !darkOLED;
    });
    setNavBarColor();
    savePreferences();
  }

  setNavBarColor() async {
    if (darkEnabled) {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
          systemNavigationBarIconBrightness: Brightness.light,
          systemNavigationBarColor:
              darkOLED ? darkThemeOLED.primaryColor : darkTheme.primaryColor));
    } else {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
          systemNavigationBarIconBrightness: Brightness.dark,
          systemNavigationBarColor: lightTheme.primaryColor));
    }
  }

  final ThemeData lightTheme = new ThemeData(
    primarySwatch: Colors.purple,
    brightness: Brightness.light,
    accentColor: Colors.purpleAccent[100],
    primaryColor: Colors.white,
    primaryColorLight: Colors.purple[700],
    textSelectionHandleColor: Colors.purple[700],
    dividerColor: Colors.grey[200],
    bottomAppBarColor: Colors.grey[200],
    buttonColor: Colors.purple[700],
    iconTheme: new IconThemeData(color: Colors.white),
    primaryIconTheme: new IconThemeData(color: Colors.black),
    accentIconTheme: new IconThemeData(color: Colors.purple[700]),
    disabledColor: Colors.grey[500],
  );

  final ThemeData darkTheme = new ThemeData(
    primarySwatch: Colors.purple,
    brightness: Brightness.dark,
    accentColor: Colors.deepPurpleAccent[100],
    primaryColor: Color.fromRGBO(50, 50, 57, 1.0),
    primaryColorLight: Colors.deepPurpleAccent[100],
    textSelectionHandleColor: Colors.deepPurpleAccent[100],
    buttonColor: Colors.deepPurpleAccent[100],
    iconTheme: new IconThemeData(color: Colors.white),
    accentIconTheme: new IconThemeData(color: Colors.deepPurpleAccent[100]),
    cardColor: Color.fromRGBO(55, 55, 55, 1.0),
    dividerColor: Color.fromRGBO(60, 60, 60, 1.0),
    bottomAppBarColor: Colors.black26,
  );

  final ThemeData darkThemeOLED = new ThemeData(
    brightness: Brightness.dark,
    accentColor: Colors.deepPurpleAccent[100],
    primaryColor: Color.fromRGBO(5, 5, 5, 1.0),
    backgroundColor: Colors.black,
    canvasColor: Colors.black,
    primaryColorLight: Colors.deepPurple[300],
    buttonColor: Colors.deepPurpleAccent[100],
    accentIconTheme: new IconThemeData(color: Colors.deepPurple[300]),
    cardColor: Color.fromRGBO(16, 16, 16, 1.0),
    dividerColor: Color.fromRGBO(20, 20, 20, 1.0),
    bottomAppBarColor: Color.fromRGBO(19, 19, 19, 1.0),
    dialogBackgroundColor: Colors.black,
    textSelectionHandleColor: Colors.deepPurpleAccent[100],
    iconTheme: new IconThemeData(color: Colors.white),
  );

  void savePreferences() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("themeMode", themeMode);
    prefs.setBool("shortenOn", shortenOn);
    prefs.setBool("darkOLED", darkOLED);
  }

  void onTapTab(int i) {
    setState(() {
      _currentIndex = i;
    });
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> _pages = [
      HomePage(),
      InvestPage(),
      /*new Tabs(
        savePreferences: savePreferences,
        toggleTheme: toggleTheme,
        handleUpdate: handleUpdate,
        darkEnabled: darkEnabled,
        themeMode: themeMode,
        switchOLED: switchOLED,
        darkOLED: darkOLED,
      ),*/
      TradePage(),
      WalletPage(),
      MyPage()
    ];
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        backgroundColor: Colors.white,
        unselectedItemColor: Colors.black45,
        selectedItemColor: Colors.blue,
        onTap: onTapTab,
        type: BottomNavigationBarType.fixed,
        selectedFontSize: 12,
        items: [
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/ic_home.png',
                width: 22,
                height: 22,
              ),
              activeIcon: Image.asset(
                'assets/images/ic_home_p.png',
                width: 22,
                height: 22,
              ),
              label: S.of(context).tab_home), //'首页'
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/ic_finance.png',
                width: 22,
                height: 22,
              ),
              activeIcon: Image.asset(
                'assets/images/ic_finance_p.png',
                width: 22,
                height: 22,
              ),
              label: S.of(context).tab_invest),//投资
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/ic_trade.png',
                width: 22,
                height: 22,
              ),
              activeIcon: Image.asset(
                'assets/images/ic_trade_p.png',
                width: 22,
                height: 22,
              ),
              label: S.of(context).tab_trade),//交易
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/ic_wallet.png',
                width: 22,
                height: 22,
              ),
              activeIcon: Image.asset(
                'assets/images/ic_wallet_p.png',
                width: 22,
                height: 22,
              ),
              label: S.of(context).tab_wallet),//钱包
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/ic_me.png',
                width: 22,
                height: 22,
              ),
              activeIcon: Image.asset(
                'assets/images/ic_me_p.png',
                width: 22,
                height: 22,
              ),
              label: S.of(context).tab_me)//我的
        ],
      ),
    );
  }
}
